#include <bits/stdc++.h>
using namespace std;
const int N = 200005, V = 1000005;
int n;
struct Christmas_tree {
	struct Node {
		long long mx, tg;
		void Tag(long long k) {
			tg += k;
			mx += k;
		}
		inline friend Node operator + (Node a, Node b) {
			return (Node){ max(a.mx, b.mx), 0 };
		}
	} tr[V << 2];
	void Clear() {
		for (int i = 1; i < (V << 2); i++)
			tr[i] = { -0x3f3f3f3f3f3f3f3f, 0 };
	}
	inline void Pushdown(int rt) {
		if (tr[rt].tg == 0)
			return ;
		tr[rt << 1].Tag(tr[rt].tg);
		tr[rt << 1 | 1].Tag(tr[rt].tg);
		tr[rt].tg = 0;
	}
	void Modify(int p, long long k, int s, int t, int rt) {
		if (s == t) {
			// tr[rt].mx = max(tr[rt].mx, k);
			tr[rt].mx = k;
			return ;
		}
		int m = (s + t) >> 1;
		Pushdown(rt);
		if (p <= m)
			Modify(p, k, s, m, rt << 1);
		else
			Modify(p, k, m + 1, t, rt << 1 | 1);
		tr[rt] = tr[rt << 1] + tr[rt << 1 | 1];
	}
	long long Query(int p, int s, int t, int rt) {
		if (s == t)
			return tr[rt].mx;
		int m = (s + t) >> 1;
		Pushdown(rt);
		if (p <= m)
			return Query(p, s, m, rt << 1);
		return Query(p, m + 1, t, rt << 1 | 1);
	}
	long long Ans(int l, int r, int s, int t, int rt) {
		if (l <= s and r >= t)
			return tr[rt].mx;
		int m = (s + t) >> 1;
		Pushdown(rt);
		long long res = LLONG_MIN;
		if (l <= m)
			res = max(res, Ans(l, r, s, m, rt << 1));
		if (r > m)
			res = max(res, Ans(l, r, m + 1, t, rt << 1 | 1));
		return res;
	}
} seg;
int a[N];
void Solve() {
	scanf("%d", &n);
	seg.Clear();
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		long long v = max(0ll, max(seg.tr[1].mx, seg.Query(a[i], 0, 1000000, 1) + a[i]));
		seg.tr[1].Tag(a[i] == a[i - 1] ? a[i] : 0),
		seg.Modify(a[i - 1], v, 0, 1000000, 1);
	}
	printf("%lld\n", seg.tr[1].mx);
}
signed main() {
	freopen("color.in", "r", stdin);
	freopen("color.out", "w", stdout);
	int t;
	scanf("%d", &t);
	while (t--)
		Solve();
	return 0;
}